package com.chenfan.mcn.mq.producer;

import com.chenfan.mcn.clients.privilege.PrivilegeClient;
import com.chenfan.mcn.clients.privilege.ScompanyRes;
import com.chenfan.mcn.constant.NumberCst;
import com.chenfan.mcn.dao.*;
import com.chenfan.mcn.enums.FinanceArapTypeEnum;
import com.chenfan.mcn.enums.FinanceChargeTypeEnum;
import com.chenfan.mcn.enums.FinanceSettTemplateEnum;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.model.*;
import com.chenfan.mcn.mq.AbstractMessageProducer;
import com.chenfan.mcn.mq.MessageProducer;
import com.chenfan.mcn.mq.message.CfChargeCommonMessage;
import com.chenfan.mcn.mq.message.ChargeAutoInvoiceMessage;
import com.chenfan.mcn.utils.BusinessAssert;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 红人月度结算单MQ消息生产者
 *
 * @author wulg
 * @date 2023/3/21
 **/
@Slf4j
@Component
public class LiveStarSettlementMessageProducer extends AbstractMessageProducer implements MessageProducer<Long> {

    @Autowired
    private LiveStarSettlementMapper settlementMapper;

    @Autowired
    private PrivilegeClient privilegeClient;

    @Autowired
    private StarBasicDataMapper starBasicDataMapper;

    @Async
    @Override
    public void asyncSendRecordToMq(Long settlementId) {
        log.info("【直播红人结算单】开始生成费用MQ消息，settlementId={}",settlementId);
        LiveStarSettlement liveStarSettlement = settlementMapper.selectById(settlementId);
        BusinessAssert.notNull(liveStarSettlement, McnErrorCode.DATA_NOT_FOUND, "查询不到结算单");

        StarBasicData starBasicData = starBasicDataMapper.selectById(liveStarSettlement.getStarId());
        BusinessAssert.notNull(starBasicData, McnErrorCode.DATA_NOT_FOUND, "查询不到该红人");
        //开票信息
        List<ScompanyRes> companyResList = privilegeClient.getParList().getObj();
        Map<Long, String> companyMap = companyResList.stream().collect(Collectors.toMap(k -> k.getCompanyId(), v -> v.getParentCompanyName()));
        CfChargeCommonMessage chargeMessage = this.buildChargeMessage(liveStarSettlement, companyMap, starBasicData);
        ChargeAutoInvoiceMessage chargeAutoInvoiceMessage = this.buildAutoInvoiceMessage(liveStarSettlement, Lists.newArrayList(chargeMessage));
        boolean result = this.doSendRecordToMq(liveStarSettlement.getSettlementCode(), chargeAutoInvoiceMessage, this.financeTopic);
        if(result) {
            //检查费用生成情况
            this.checkCharge(liveStarSettlement.getSettlementCode());
        }
    }

    /**
     * 构建费用消息
     * @return
     */
    private  CfChargeCommonMessage buildChargeMessage(LiveStarSettlement liveStarSettlement, Map<Long, String> companyMap, StarBasicData starBasicData) {
        CfChargeCommonMessage chargeMessage = new CfChargeCommonMessage();
        chargeMessage.setChargeType(FinanceChargeTypeEnum.LIVE_STAR_SETTLEMENT_CHARGE.getType());
        chargeMessage.setArapType(FinanceArapTypeEnum.PAY.getType());
        chargeMessage.setChargeSourceCode(liveStarSettlement.getSettlementCode());
        chargeMessage.setAmountPp(liveStarSettlement.getPaymentAmount());
        chargeMessage.setPricePp(chargeMessage.getAmountPp());
        //红人收款账户的【户名】
        chargeMessage.setBalance(starBasicData.getStarNickname());
        //子结算单 ：我司签约主体
        chargeMessage.setFinanceEntity(companyMap.get(liveStarSettlement.getContractingId()));
        chargeMessage.setCreateBy(liveStarSettlement.getCreateBy());
        chargeMessage.setCreateName(liveStarSettlement.getCreateName());
        chargeMessage.setCreateDate(new Date());
        chargeMessage.setUpdateBy(chargeMessage.getCreateBy());
        chargeMessage.setUpdateDate(chargeMessage.getCreateDate());
        chargeMessage.setUpdateName(chargeMessage.getCreateName());
        chargeMessage.setTenantId(liveStarSettlement.getTenantId());
        chargeMessage.setCompanyId(liveStarSettlement.getCompanyId());
        chargeMessage.setDepartmentId(liveStarSettlement.getDepartmentId());
        chargeMessage.setSettTemplate(NumberCst.SIX);
        chargeMessage.setSettTemplate(FinanceSettTemplateEnum.LIVE_STAR_SETTLEMENT.getType());
        chargeMessage.setTaxRate(BigDecimal.ZERO);

        chargeMessage.setAccountId(liveStarSettlement.getStarAccountId());
        chargeMessage.setBalanceType(NumberCst.ONE);
        return chargeMessage;
    }

    /**
     * 构建自动生成账单报文
     * @param chargeList
     * @return
     */
    private ChargeAutoInvoiceMessage buildAutoInvoiceMessage(LiveStarSettlement liveStarSettlement, List<CfChargeCommonMessage> chargeList) {
        ChargeAutoInvoiceMessage message = new ChargeAutoInvoiceMessage();
        //业务类型：1-货品采购; 2-销售订单; 3-MCN; 4-WMS
        message.setChargeSourceType(3);
        message.setChargeInfos(chargeList);
        message.setCreateBy(liveStarSettlement.getCreateBy());
        message.setCreateName(liveStarSettlement.getCreateName());
        message.setCreateDate(new Date());
        message.setUpdateBy(liveStarSettlement.getCreateBy());
        message.setUpdateName(liveStarSettlement.getCreateName());
        message.setUpdateDate(liveStarSettlement.getCreateDate());
        message.setTenantId(liveStarSettlement.getTenantId());
        message.setCompanyId(liveStarSettlement.getCompanyId());
        message.setDepartmentId(liveStarSettlement.getDepartmentId());
         return message;
    }
}
